﻿#include <QFile>
#include <QStringList>
#include "CTaskRuleDlg.h"
#include "CHeader.h"
#include "CSqlOper.h"
#include "CLogOper.h"
#include <QButtonGroup>
CTaskRuleDlg::CTaskRuleDlg(QString sSceneGuid, QString sNewTaskGuid, QString sNewTaskName, int nPeriodType, QWidget *parent)
	: m_sGuid(sSceneGuid),m_sNewTaskGuid(sNewTaskGuid),m_sNewTaskName(sNewTaskName),m_nPeriodType(nPeriodType),QDialog(parent)
{
	ui.setupUi(this);
	setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
	setTitle(A2T("任务规则"));
	setStyleSheetFile();
	initData();
	setData();
	initConnect();
}

CTaskRuleDlg::~CTaskRuleDlg()
{}

void CTaskRuleDlg::setData()
{
	ui.tableWidget->setColumnCount(2);//设置两列
	QStringList list;
	list<< A2T("时间") << A2T("替换规则");
	ui.tableWidget->verticalHeader()->setHidden(true);
	ui.tableWidget->setHorizontalHeaderLabels(list);//设置标题
	ui.tableWidget->setColumnWidth(0, 50);
	ui.tableWidget->setRowCount(m_vecTimeType.size());
	//行末尾自动拉伸填充
	ui.tableWidget->horizontalHeader()->setStretchLastSection(true);
	//查询数据
	QString sErrMsg = "";
	QVector<QSqlRecord> vecRecord;
	bool bRet=selectTaskRule(m_sGuid,vecRecord,sErrMsg);
	if (bRet == false)
	{
		SExecLog log;
		log.m_nAlarmLevel = enSysAlarm;
		log.m_sFuncName = A2T("数据库");
		log.m_sDescriptionl = sErrMsg;
		CLogOper::instance().addMsg(log,m_sGuid);
	}
	QMap<int, QString> mapTaskRule;
	//填写表格
	for (int i = 0; i < vecRecord.size(); i++)
	{
		int nTimeType = vecRecord[i].value("TIME_TYPE").toInt();
		QString sTaskGuid= vecRecord[i].value("TASK_GUID").toString();
		mapTaskRule.insert(nTimeType, sTaskGuid);
	}

	for (int i = 0; i < m_vecTimeType.size(); i++)
	{
		// 时间
		QTableWidgetItem *pIndex = new QTableWidgetItem(m_mapTimeType[m_vecTimeType[i]]);
		ui.tableWidget->setItem(i, 0, pIndex);
		QString sRowData = "";
		sRowData = QString::number(m_vecTimeType[i]) + "-" + m_mapTaskName[mapTaskRule[m_vecTimeType[i]]] + "-" + m_sNewTaskGuid;
		pIndex->setData(Qt::UserRole + 100, sRowData);

		// 数据（QWidget）
		QWidget *dataWidget = new QWidget(ui.tableWidget);
		QHBoxLayout* pLayout = new QHBoxLayout(dataWidget);
		dataWidget->setLayout(pLayout);
		QCheckBox* pCheckBox = new QCheckBox(dataWidget);
		m_mapChecBox.insert(pCheckBox, i);
		connect(pCheckBox, &QCheckBox::clicked, this, &CTaskRuleDlg::slotAddToList);
		pLayout->addWidget(pCheckBox);
		QLabel* pLabelOld = new QLabel(dataWidget);
		pLabelOld->setText(m_mapTaskName[mapTaskRule[m_vecTimeType[i]]]);
		pLayout->addWidget(pLabelOld);
		QLabel* pLabel = new QLabel(dataWidget);
		pLabel->setText("<=");
		pLayout->addWidget(pLabel);
		QLabel* pLabelNew = new QLabel(dataWidget);
		pLabelNew->setText(m_sNewTaskName);
		pLayout->addWidget(pLabelNew);
		ui.tableWidget->setCellWidget(i, 1, dataWidget);

		//设置每行的行高
		ui.tableWidget->setRowHeight(i, 50);
	}
}

void CTaskRuleDlg::setTitle(QString sTitle)
{
	this->setWindowTitle(sTitle);
}

void CTaskRuleDlg::setStyleSheetFile()
{
	//设置对话框标题
	this->setWindowTitle(A2T("任务配置"));
	QString sQssPath = ":/qss/styleSheet.css";
	QFile qss(sQssPath);
	qss.open(QFile::ReadOnly);
	this->setStyleSheet(qss.readAll());
}

void CTaskRuleDlg::initConnect()
{
	connect(ui.pushButtonSave, &QPushButton::clicked, this, &CTaskRuleDlg::slotPushButtonSaveClicked);
	connect(ui.pushButtonCancle, &QPushButton::clicked, this, &CTaskRuleDlg::slotPushButtonCancleClicked);
}

void CTaskRuleDlg::initData()
{
	m_mapTimeType.insert(0, A2T("00:00"));
	m_mapTimeType.insert(1, A2T("00:30"));
	m_mapTimeType.insert(2, A2T("01:00"));
	m_mapTimeType.insert(3, A2T("01:30"));
	m_mapTimeType.insert(4, A2T("02:00"));
	m_mapTimeType.insert(5, A2T("02:30"));
	m_mapTimeType.insert(6, A2T("03:00"));
	m_mapTimeType.insert(7, A2T("03:30"));
	m_mapTimeType.insert(8, A2T("04:00"));
	m_mapTimeType.insert(9, A2T("04:30"));

	m_mapTimeType.insert(10, A2T("05:00"));
	m_mapTimeType.insert(11, A2T("05:30"));
	m_mapTimeType.insert(12, A2T("06:00"));
	m_mapTimeType.insert(13, A2T("06:30"));
	m_mapTimeType.insert(14, A2T("07:00"));
	m_mapTimeType.insert(15, A2T("07:30"));
	m_mapTimeType.insert(16, A2T("08:00"));
	m_mapTimeType.insert(17, A2T("08:30"));
	m_mapTimeType.insert(18, A2T("09:00"));
	m_mapTimeType.insert(19, A2T("09:30"));

	m_mapTimeType.insert(20, A2T("10:00"));
	m_mapTimeType.insert(21, A2T("10:30"));
	m_mapTimeType.insert(22, A2T("11:00"));
	m_mapTimeType.insert(23, A2T("11:30"));
	m_mapTimeType.insert(24, A2T("12:00"));
	m_mapTimeType.insert(25, A2T("12:30"));
	m_mapTimeType.insert(26, A2T("13:00"));
	m_mapTimeType.insert(27, A2T("13:30"));
	m_mapTimeType.insert(28, A2T("14:00"));
	m_mapTimeType.insert(29, A2T("14:30"));

	m_mapTimeType.insert(30, A2T("15:00"));
	m_mapTimeType.insert(31, A2T("15:30"));
	m_mapTimeType.insert(32, A2T("16:00"));
	m_mapTimeType.insert(33, A2T("16:30"));
	m_mapTimeType.insert(34, A2T("17:00"));
	m_mapTimeType.insert(35, A2T("17:30"));
	m_mapTimeType.insert(36, A2T("18:00"));
	m_mapTimeType.insert(37, A2T("18:30"));
	m_mapTimeType.insert(38, A2T("19:00"));
	m_mapTimeType.insert(39, A2T("19:30"));

	m_mapTimeType.insert(40, A2T("20:00"));
	m_mapTimeType.insert(41, A2T("20:30"));
	m_mapTimeType.insert(42, A2T("21:00"));
	m_mapTimeType.insert(43, A2T("21:30"));
	m_mapTimeType.insert(44, A2T("22:00"));
	m_mapTimeType.insert(45, A2T("22:30"));
	m_mapTimeType.insert(46, A2T("23:00"));
	m_mapTimeType.insert(47, A2T("23:30"));

	//根据传入的周期类型计算需要的时间点
	//周期类型(0:12h 1:6h 2:4h 3:2h 4:1h 5:0.5h)
	if (m_nPeriodType == 0)
	{
		m_vecTimeType.push_back(0);//00:00
		m_vecTimeType.push_back(24);//12:00
	}
	else if (m_nPeriodType == 1)
	{
		m_vecTimeType.push_back(0);//00:00
		m_vecTimeType.push_back(12);//06:00
		m_vecTimeType.push_back(24);//12:00
		m_vecTimeType.push_back(36);//18:00
	}
	else if (m_nPeriodType == 2)
	{
		m_vecTimeType.push_back(0);//00:00
		m_vecTimeType.push_back(8);//04:00
		m_vecTimeType.push_back(16);//08:00
		m_vecTimeType.push_back(24);//12:00
		m_vecTimeType.push_back(32);//16:00
		m_vecTimeType.push_back(40);//20:00
	}
	else if (m_nPeriodType == 3)
	{
		m_vecTimeType.push_back(0);//00:00
		m_vecTimeType.push_back(4);//02:00
		m_vecTimeType.push_back(8);//04:00
		m_vecTimeType.push_back(12);//06:00
		m_vecTimeType.push_back(16);//08:00
		m_vecTimeType.push_back(20);//10:00
		m_vecTimeType.push_back(24);//12:00
		m_vecTimeType.push_back(28);//14:00
		m_vecTimeType.push_back(32);//16:00
		m_vecTimeType.push_back(36);//18:00
		m_vecTimeType.push_back(40);//20:00
		m_vecTimeType.push_back(44);//22:00
	}
	else if (m_nPeriodType == 4)
	{
		m_vecTimeType.push_back(0);//00:00
		m_vecTimeType.push_back(2);//01:00
		m_vecTimeType.push_back(4);//02:00
		m_vecTimeType.push_back(6);//03:00
		m_vecTimeType.push_back(8);//04:00
		m_vecTimeType.push_back(10);//05:00
		m_vecTimeType.push_back(12);//06:00
		m_vecTimeType.push_back(14);//07:00
		m_vecTimeType.push_back(16);//08:00
		m_vecTimeType.push_back(18);//09:00
		m_vecTimeType.push_back(20);//10:00
		m_vecTimeType.push_back(22);//11:00
		m_vecTimeType.push_back(24);//12:00
		m_vecTimeType.push_back(26);//13:00
		m_vecTimeType.push_back(28);//14:00
		m_vecTimeType.push_back(30);//15:00
		m_vecTimeType.push_back(32);//16:00
		m_vecTimeType.push_back(34);//17:00
		m_vecTimeType.push_back(36);//18:00
		m_vecTimeType.push_back(38);//19:00
		m_vecTimeType.push_back(40);//20:00
		m_vecTimeType.push_back(42);//21:00
		m_vecTimeType.push_back(44);//22:00
		m_vecTimeType.push_back(46);//23:00
	}
	else if (m_nPeriodType == 5)
	{
		m_vecTimeType.push_back(0);//00:00
		m_vecTimeType.push_back(1);//00:30
		m_vecTimeType.push_back(2);//01:00
		m_vecTimeType.push_back(3);//01:30
		m_vecTimeType.push_back(4);//02:00
		m_vecTimeType.push_back(5);//02:30
		m_vecTimeType.push_back(6);//03:00
		m_vecTimeType.push_back(7);//03:30
		m_vecTimeType.push_back(8);//04:00
		m_vecTimeType.push_back(9);//04:30
		m_vecTimeType.push_back(10);//05:00
		m_vecTimeType.push_back(11);//05:30
		m_vecTimeType.push_back(12);//06:00
		m_vecTimeType.push_back(13);//06:30
		m_vecTimeType.push_back(14);//07:00
		m_vecTimeType.push_back(15);//07:30
		m_vecTimeType.push_back(16);//08:00
		m_vecTimeType.push_back(17);//08:30
		m_vecTimeType.push_back(18);//09:00
		m_vecTimeType.push_back(19);//09:30
		m_vecTimeType.push_back(20);//10:00
		m_vecTimeType.push_back(21);//10:30
		m_vecTimeType.push_back(22);//11:00
		m_vecTimeType.push_back(23);//11:30
		m_vecTimeType.push_back(24);//12:00
		m_vecTimeType.push_back(25);//12:30
		m_vecTimeType.push_back(26);//13:00
		m_vecTimeType.push_back(27);//13:30
		m_vecTimeType.push_back(28);//14:00
		m_vecTimeType.push_back(29);//14:30
		m_vecTimeType.push_back(30);//15:00
		m_vecTimeType.push_back(31);//15:30
		m_vecTimeType.push_back(32);//16:00
		m_vecTimeType.push_back(33);//16:30
		m_vecTimeType.push_back(34);//17:00
		m_vecTimeType.push_back(35);//17:30
		m_vecTimeType.push_back(36);//18:00
		m_vecTimeType.push_back(37);//18:30
		m_vecTimeType.push_back(38);//19:00
		m_vecTimeType.push_back(39);//19:30
		m_vecTimeType.push_back(40);//20:00
		m_vecTimeType.push_back(41);//20:30
		m_vecTimeType.push_back(42);//21:00
		m_vecTimeType.push_back(43);//21:30
		m_vecTimeType.push_back(44);//22:00
		m_vecTimeType.push_back(45);//22:30
		m_vecTimeType.push_back(46);//23:00
		m_vecTimeType.push_back(47);//23:30
	}

	QString sErrMsg = "";
	QVector<CTaskInfo> vecTaskInfo=selectTaskInfoCycTask(m_sGuid, sErrMsg);
	for (int i = 0; i < vecTaskInfo.size(); i++)
	{
		m_mapTaskName.insert(vecTaskInfo[i].m_sTaskGuid, vecTaskInfo[i].m_sTaskName);
	}
}

void CTaskRuleDlg::slotPushButtonSaveClicked()
{
	QVector<int> vecTaskType;//需要更新的时间点
	for (QMap<int, SReplaceData>::iterator itor = m_mapReplaceData.begin(); itor != m_mapReplaceData.end(); itor++)
	{
		int nTimeType = itor.key();
		vecTaskType.push_back(nTimeType);
	}
	QString sErrMsg = "";
	bool bRet=updateTaskRule(m_sGuid, vecTaskType,m_sNewTaskGuid, sErrMsg);
	if (bRet)
	{
		this->accept();
	}
	else
	{
		this->close();
	}
}

void CTaskRuleDlg::slotPushButtonCancleClicked()
{
	this->close();
}

//槽函数添加到替换列表
void CTaskRuleDlg::slotAddToList()
{
	QCheckBox* pCheckBox = qobject_cast<QCheckBox*>(sender());
	int nRow = m_mapChecBox.value(pCheckBox);
	QString sRowData = ui.tableWidget->item(nRow, 0)->data(Qt::UserRole + 100).toString();
	QStringList list = sRowData.split("-");
	int nTimeType = list[0].toInt();
	QString sOldTaskGuid = list[1];
	QString sNewTaskGuid = list[2];
	if (m_mapReplaceData.contains(nTimeType))
	{
		m_mapReplaceData.remove(nTimeType);
	}
	else
	{
		SReplaceData stData;
		stData.m_nTimeType = nTimeType;
		stData.m_sNewTaskGuid = sNewTaskGuid;
		stData.m_sOldTaskGuid = sOldTaskGuid;
		m_mapReplaceData.insert(nTimeType, stData);
	}
}
